package com.gemcoder.system.service;

import static com.gemcoder.system.datasource.JdbcTemplateUtil.executeSQL;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserName;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserId;
import com.gemcoder.system.bo.Result;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllPermissions;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllMenus;
import java.sql.SQLException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import cn.hutool.core.util.StrUtil;

@Service
public class SparepartsmanagementService {

    public Result searchSpareParts(Map<String, Object> paramMap) {
        String projectName = paramMap.get("projectName") != null ? String.valueOf(paramMap.get("projectName")) : null;
        Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;
        StringBuilder sql = new StringBuilder("SELECT id, serial_number, category, name, unit, inbound_quantity, outbound_quantity, (inbound_quantity - outbound_quantity) AS stock_quantity FROM spare_parts_management WHERE 1=1");
        List<Object> params = new ArrayList<>();
        if (projectName != null && !projectName.isEmpty()) {
            sql.append(" AND name LIKE ?");
            params.add("%" + projectName + "%");
        }
        sql.append(" LIMIT ?, ?");
        params.add((current - 1) * pageSize);
        params.add(pageSize);
        List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());
        StringBuilder countSql = new StringBuilder("SELECT COUNT(*) AS total FROM spare_parts_management WHERE 1=1");
        List<Object> countParams = new ArrayList<>();
        if (projectName != null && !projectName.isEmpty()) {
            countSql.append(" AND name LIKE ?");
            countParams.add("%" + projectName + "%");
        }
        List<Map<String, Object>> countResult = executeSQL(countSql.toString(), countParams.toArray());
        int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("data", data);
        return new Result(true, "查询成功", result);
    }

    public Result getAllSpareParts(Map<String, Object> paramMap) {
        Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;
        StringBuilder sql = new StringBuilder("SELECT id, serial_number, category, name, unit, inbound_quantity, outbound_quantity, (inbound_quantity - outbound_quantity) AS stock_quantity FROM spare_parts_management LIMIT ?, ?");
        List<Object> params = new ArrayList<>();
        params.add((current - 1) * pageSize);
        params.add(pageSize);
        List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());
        StringBuilder countSql = new StringBuilder("SELECT COUNT(*) AS total FROM spare_parts_management");
        List<Map<String, Object>> countResult = executeSQL(countSql.toString());
        int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("data", data);
        return new Result(true, "查询成功", result);
    }

    public Result addSparePart(Map<String, Object> paramMap) {
        String category = paramMap.get("Categoryvalue1") != null ? String.valueOf(paramMap.get("Categoryvalue1")) : null;
        String name = paramMap.get("Namevalue1") != null ? String.valueOf(paramMap.get("Namevalue1")) : null;
        String unit = paramMap.get("Unitvalue1") != null ? String.valueOf(paramMap.get("Unitvalue1")) : null;
        Integer inboundQuantity = paramMap.get("Quantityinstoragevalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Quantityinstoragevalue1"))) : 0;
        Integer outboundQuantity = paramMap.get("Outboundquantityvalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Outboundquantityvalue1"))) : 0;
        String createUsername = sdkOfGetCurrentUserName();
        StringBuilder sql = new StringBuilder("INSERT INTO spare_parts_management (category, name, unit, inbound_quantity, outbound_quantity, stock_quantity, create_username, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, NOW())");
        List<Object> params = new ArrayList<>();
        params.add(category);
        params.add(name);
        params.add(unit);
        params.add(inboundQuantity);
        params.add(outboundQuantity);
        params.add(inboundQuantity - outboundQuantity);
        params.add(createUsername);
        int rows = executeSQL(sql.toString(), params.toArray());
        if (rows > 0) {
            return new Result(true, "新增成功", null);
        } else {
            return new Result(false, "新增失败", null);
        }
    }

    public Result updateSparePart(Map<String, Object> paramMap) {
        Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
        String category = paramMap.get("Categoryvalue2") != null ? String.valueOf(paramMap.get("Categoryvalue2")) : null;
        String name = paramMap.get("Namevalue2") != null ? String.valueOf(paramMap.get("Namevalue2")) : null;
        String unit = paramMap.get("Unitvalue2") != null ? String.valueOf(paramMap.get("Unitvalue2")) : null;
        Integer inboundQuantity = paramMap.get("Quantityinstoragevalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Quantityinstoragevalue2"))) : 0;
        Integer outboundQuantity = paramMap.get("Outboundquantityvalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Outboundquantityvalue2"))) : 0;
        String updateUsername = sdkOfGetCurrentUserName();
        StringBuilder sql = new StringBuilder("UPDATE spare_parts_management SET category = ?, name = ?, unit = ?, inbound_quantity = ?, outbound_quantity = ?, stock_quantity = ?, update_username = ?, update_time = NOW() WHERE id = ?");
        List<Object> params = new ArrayList<>();
        params.add(category);
        params.add(name);
        params.add(unit);
        params.add(inboundQuantity);
        params.add(outboundQuantity);
        params.add(inboundQuantity - outboundQuantity);
        params.add(updateUsername);
        params.add(id);
        int rows = executeSQL(sql.toString(), params.toArray());
        if (rows > 0) {
            return new Result(true, "更新成功", null);
        } else {
            return new Result(false, "更新失败", null);
        }
    }

    public Result deleteSparePart(Map<String, Object> paramMap) {
        Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
        StringBuilder sql = new StringBuilder("DELETE FROM spare_parts_management WHERE id = ?");
        List<Object> params = new ArrayList<>();
        params.add(id);
        int rows = executeSQL(sql.toString(), params.toArray());
        if (rows > 0) {
            return new Result(true, "删除成功", null);
        } else {
            return new Result(false, "删除失败", null);
        }
    }

    public Result getAllSparePartsOZM(Map<String, Object> paramMap) {
        // 获取分页参数
        Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
        Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;
        // 计算分页的起始位置
        int offset = (current - 1) * pageSize;
        // 构建查询总条数的SQL
        String countSql = "SELECT COUNT(*) AS total FROM spare_parts_management";
        List<Map<String, Object>> countResult = executeSQL(countSql);
        int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;
        // 构建分页查询的SQL
        StringBuilder sql = new StringBuilder("SELECT id, serial_number, category, name, unit, inbound_quantity, outbound_quantity, stock_quantity, create_username, create_time, update_username, update_time FROM spare_parts_management LIMIT ?, ?");
        List<Object> params = new ArrayList<>();
        params.add(offset);
        params.add(pageSize);
        // 执行查询
        List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());
        // 构建返回结果
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("total", total);
        resultData.put("data", data);
        // 返回结果
        return new Result(true, "查询成功", resultData);
    }
}
